from fastapi import APIRouter,Depends,Form
from sqlalchemy.orm import Session
from utils import token
from extend.get_db import get_db
from models.permission.permission_operation import get_permission_pagenation, get_permission_total, \
    get_permission_by_id, get_no_parent_names, permission_update, delete_permission_by_id, get_parents_names, \
    add_permission, query_permission, get_permission_query_total
from models.permission.permission_ret import PermissionRet
from fastapi.responses import JSONResponse


from models.department.department_ret import DepartmentRet

router=APIRouter(
    prefix="/permission"
)


@router.get("/permission_list",tags=["权限模块"])
def get_permission_list(page_size:int,current_page:int,id:str = Depends(token.parse_token),db:Session = Depends(get_db)):
    permissions = get_permission_pagenation(db,page_size,current_page)
    total = get_permission_total(db)
    permission_list = []
    for permission in permissions:
        permission_ret = PermissionRet()
        permission_ret.id=permission.id
        permission_ret.name=permission.name
        permission_ret.url=permission.url
        permission_ret.method=permission.method
        permission_ret.args=permission.args
        permission_ret.desc=permission.desc
        permission_ret.create_time=permission.create_time



        if permission.parent_id == 0:
            permission_ret.level="一级"
            permission_ret.parent_name = "无"
        else:
            permission_ret.level = "二级"
            p = get_permission_by_id(db, permission.parent_id)
            permission_ret.parent_name = p.name
        permission_list.append(permission_ret)
    content = {
        "permissions":permission_list,
        "pageSize":page_size,
        "pageTotal":total,
        "currentPage":current_page
    }
    return content


#用户修改，涉及到图片上次，所有得用FormData这种方式提交
@router.post("/edit",tags=["权限模块"])
def edit(
        id:int=Form(...),
        name:str=Form(...),
        url:str=Form(...),
        method:str= Form(...),
        args: str=Form(None),
        parent_name:str= Form(...),
        desc: str=Form(...),
        icon: str=Form(...),
        user_id:str = Depends(token.parse_token),
        db:Session = Depends(get_db)):
    permission_update(db,id,name,url,method,args,parent_name,desc,icon)
    return {"code":200,"msg":"更新成功","id":id}

@router.get("/get_no_parents",tags=["权限模块"])
def get_no_parents(id:int,user_id:str = Depends(token.parse_token),db:Session = Depends(get_db)):
    parents=get_no_parent_names(db,id)
    return {"code": 200, "msg": "更新成功", "parents": parents}



@router.get("/get_parents",tags=["权限模块"])
def get_parents(user_id:str = Depends(token.parse_token),db:Session = Depends(get_db)):
    parents=get_parents_names(db)
    return {"code": 200, "msg": "更新成功", "parents": parents}


@router.post("/delete",tags=["权限模块"])
def delete_permission(permission:PermissionRet,user_id:str = Depends(token.parse_token),
        db:Session = Depends(get_db)):
    id = permission.id
    delete_permission_by_id(db,id)
    return JSONResponse(content={"code":200,"msg":"删除成功","id":id})


@router.post("/add",tags=["权限模块"])
async def add(
        name: str = Form(...),
        url: str = Form(...),
        method: str = Form(...),
        args: str = Form(None),
        parent_name: str = Form(...),
        desc: str = Form(...),
        icon: str = Form(...),
        user_id:str = Depends(token.parse_token),
        db:Session = Depends(get_db)):
    add_permission(db,name,url,args,method,parent_name,desc,icon)
    return JSONResponse(content={"code":200,"msg":"添加成功"})


# 查询
@router.get("/query",tags=["权限模块"])
def query(
        q:str,
        page_size:int,
        current_page:int,
        user_id: str = Depends(token.parse_token),
        db: Session = Depends(get_db)):

    if q == "" or q == None or q == '':

        roles = get_permission_pagenation(db,page_size,current_page)
        total = get_permission_total(db)
    else:
        q = q.strip()
        permissions = query_permission(db,q,page_size,current_page)
        total = get_permission_query_total(db,q)
    permission_list = []
    for permission in permissions:
        permission_ret = PermissionRet()
        permission_ret.id = permission.id
        permission_ret.name = permission.name
        permission_ret.url = permission.url
        permission_ret.method = permission.method
        permission_ret.args = permission.args
        permission_ret.desc = permission.desc
        permission_ret.create_time = permission.create_time

        if permission.parent_id == 0:
            permission_ret.level = "一级"
            permission_ret.parent_name = "无"
        else:
            permission_ret.level = "二级"
            p = get_permission_by_id(db, permission.parent_id)
            permission_ret.parent_name = p.name
        permission_list.append(permission_ret)
    content = {
        "permissions":permission_list,
        "pageSize":page_size,
        "pageTotal":total,
        "currentPage":current_page
    }
    return content



